<!--
  * 捐赠方信息维护
-->
<template>
    <!---------- 查询表单form begin ----------->
    <a-form class="smart-query-form">
        <a-row class="smart-query-form-row">
            <a-form-item label="关键字查询" class="smart-query-form-item">
                <a-input style="width: 200px" v-model:value="queryForm.key" placeholder="关键字查询" />
            </a-form-item>
            <a-form-item class="smart-query-form-item">
                <a-button type="primary" @click="queryData('1')">
                    <template #icon>
                        <SearchOutlined />
                    </template>
                    查询
                </a-button>
                <a-button @click="resetQuery" class="smart-margin-left10">
                    <template #icon>
                        <ReloadOutlined />
                    </template>
                    重置
                </a-button>
            </a-form-item>
        </a-row>
    </a-form>
    <!---------- 查询表单form end ----------->

    <a-card size="small" :bordered="false" :hoverable="true">
        <!---------- 表格操作行 begin ----------->
        <a-row class="smart-table-btn-block">
            <div class="smart-table-operate-block">
                <a-button @click="showForm" type="primary">
                    <template #icon>
                        <PlusOutlined />
                    </template>
                    新建
                </a-button>
                <a-button @click="confirmBatchDelete" type="primary" danger :disabled="selectedRowKeyList.length === 0">
                    <template #icon>
                        <DeleteOutlined />
                    </template>
                    批量删除
                </a-button>
            </div>
            <span class="smart-table-column-operate">
                <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.SYSTEM.EMPLOYEE" :refresh="queryData" />
            </span>
        </a-row>
        <!---------- 表格操作行 end ----------->

        <!---------- 表格 begin ----------->
        <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="bldonorid" bordered
            :loading="tableLoading" :pagination="false" :scroll="{ x: 1500}"
            :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }">
            <template #bodyCell="{ text, record, column }">
                <template v-if="column.dataIndex === 'bldonor'">
                    <a @click="showFormView(record)">{{ record.bldonor }}</a>
                </template>
                <template v-if="column.dataIndex === 'action'">
                    <div class="smart-table-operate">
                        <a-button type="primary" @click="showForm(record)" size="small">
                            <FormOutlined />
                        </a-button>
                        <a-button style="margin-left: 10px;" danger size="small" @click="onDelete(record)">
                            <DeleteOutlined />
                        </a-button>
                    </div>
                </template>
            </template>
        </a-table>
        <!---------- 表格 end ----------->
        <!-- 分页-->
        <div class="smart-query-table-page">
            <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS"
                :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit"
                :total="total" @change="queryData" @showSizeChange="queryData" :show-total="(total) => `共${total}条`" />
        </div>
        <DonationOperate ref="formRef" @refresh="queryData" />
    </a-card>
</template>
<script setup>
    import { reactive, ref, onMounted } from 'vue';
    import { message, Modal } from 'ant-design-vue';
    import { SmartLoading } from '/@/components/framework/smart-loading';
    import { DonaInfoApi } from '/@/api/basicManage/DonationManage/DonaInfo-api';
    import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
    import { smartSentry } from '/@/lib/smart-sentry';
    import DonationOperate from './modules/DonaInfo-operate.vue';
    import _ from 'lodash';
    import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
    import TableOperator from '/@/components/support/table-operator/index.vue';
    // ---------------------------- 表格列 ----------------------------

    const columns = ref([
        {
            title: '捐赠方名称',
            dataIndex: 'bldonor',
            align: 'center',
            fixed: 'left',
        },
        {
            title: '省份',
            dataIndex: 'blprovince',
            align: 'center',
        },
        {
            title: '电话',
            dataIndex: 'blphone',
            align: 'center',
        },
        {
            title: '邮箱',
            dataIndex: 'blemail',
            align: 'center',
        },
        {
            title: '捐赠方情况说明',
            dataIndex: 'bldescription',
            align: 'center',
        },
        {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            width: 100,
        },
    ]);

    // ---------------------------- 查询数据表单和方法 ----------------------------

    const queryFormState = {
        key: undefined,
        page: 1,
        limit: PAGE_SIZE,
    };
    // 查询表单form
    const queryForm = reactive({ ...queryFormState });
    // 表格加载loading
    const tableLoading = ref(false);
    // 表格数据
    const tableData = ref([]);
    // 总数
    const total = ref(0);

    // 重置查询条件
    function resetQuery() {
        let pageSize = queryForm.limit;
        Object.assign(queryForm, queryFormState);
        queryForm.limit = pageSize;
        queryData();
        searchPage.value = false
    }
    const searchPage = ref(false)//检索的翻页
    // 查询数据
    async function queryData(a) {
        tableLoading.value = true;
        try {
            if (a === '1') {
                queryForm.page = 1;
                searchPage.value = true
            }
            if (!searchPage.value) {
                queryForm.key = undefined;
            }
            let queryResult = await DonaInfoApi.foundationList(queryForm);
            tableData.value = queryResult.page.list;
            total.value = queryResult.page.totalCount;
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            tableLoading.value = false;
        }
    }

    onMounted(queryData);

    // ---------------------------- 添加/修改 ----------------------------
    const formRef = ref();

    function showForm(data) {
        const newObj = Object.assign(data, { showView: false });
        formRef.value.showModal(newObj);
    }
    // 查看  

    function showFormView(data) {
        const newObj = Object.assign(data, { showView: true });
        formRef.value.showViewModal(newObj);
    }

    // ---------------------------- 单个删除 ----------------------------
    //确认删除
    function onDelete(data) {
        Modal.confirm({
            title: '提示',
            content: '确定要删除吗?',
            okText: '删除',
            okType: 'danger',
            onOk() {
                requestDelete(data);
            },
            cancelText: '取消',
            onCancel() { },
        });
    }

    //请求删除
    async function requestDelete(data) {
        SmartLoading.show();
        try {
            let array = [data.bldonorid];
            await DonaInfoApi.foundationDelete(array);
            message.success('删除成功');
            const totalPage = Math.ceil(
                (total.value - 1) / PAGE_SIZE
            );
            const currentPage =
                queryForm.page > totalPage ? totalPage : queryForm.page;
            queryForm.page = currentPage < 1 ? 1 : currentPage;
            queryData();
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            SmartLoading.hide();
        }
    }

    // ---------------------------- 批量删除 ----------------------------

    // 选择表格行
    const selectedRowKeyList = ref([]);

    function onSelectChange(selectedRowKeys) {
        selectedRowKeyList.value = selectedRowKeys;
    }

    // 批量删除
    function confirmBatchDelete() {
        if (_.isEmpty(selectedRowKeyList.value)) {
            message.success('请选择要删除的数据');
            return;
        }
        Modal.confirm({
            title: '提示',
            content: '确定要批量删除这些数据吗?',
            okText: '删除',
            okType: 'danger',
            onOk() {
                requestBatchDelete();
            },
            cancelText: '取消',
            onCancel() { },
        });
    }

    //请求批量删除
    async function requestBatchDelete() {
        try {
            SmartLoading.show();
            await DonaInfoApi.foundationDelete(selectedRowKeyList.value);
            selectedRowKeyList.value.length = 0
            message.success('删除成功');
            const totalPage = Math.ceil(
                (total.value - 1) / PAGE_SIZE
            );
            const currentPage =
                queryForm.page > totalPage ? totalPage : queryForm.page;
            queryForm.page = currentPage < 1 ? 1 : currentPage;
            queryData();
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            SmartLoading.hide();
        }
    }
</script>